package com.kingstar.ceres.asny;

import com.kingstar.ceres.memory.MessageMemory;
import com.kingstar.entity.BasicQuote;
import com.kingstar.format.cmds.local.*;
import imix.FieldNotFound;
import imix.IncorrectTagValue;
import imix.UnsupportedMessageType;
import imix.field.*;
import imix.imix10.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class ParseAndSendToSave {

    @Async("parseExecutor")
    public void parseMessageAndSaveToDB(Message message){
        if (message==null){
            log.warn("message is null");
            return;
        }
        try {
            BasicQuote basicQuote = null;
            if (message instanceof MarketDataSnapshotFullRefresh) {
                MarketDataSnapshotFullRefresh mdMessage = (MarketDataSnapshotFullRefresh) message;

                //List<Group> list = md.getGroups(NoMDTypes.FIELD);

                MarketDataSnapshotFullRefresh.NoMDTypes types = new MarketDataSnapshotFullRefresh.NoMDTypes();

                int count = message.getGroupCount(NoMDTypes.FIELD);
                for (int i = 1; i <= count; i++) {
                    message.getGroup(i, types);

                    //R --人民币市场行情
                    char mdType = types.getChar(MDType.FIELD);

                    //人民币行情
                    if(mdType == MDType.RMB_MARKET_DATA_STATISTICS)
                    {
                        //4-现券买卖
                        String marketInd = types.getString(MarketIndicator.FIELD);
                        // 现券买卖行情
                        if (marketInd.equals(MarketIndicator.CASH_BOND)){
                            //0--成交行情
                            //7-交易品种行情
                            int mdSubType = types.getInt(MDSubType.FIELD);

                            //0-汇总
                            //3-匿名点击
                            int tradeMethod = types.getInt(TradeMethod.FIELD);


                            //匿名点击 (成交行情xbond)
                            if (tradeMethod== TradeMethod.MATCHING_NORMAL  && mdSubType ==MDSubType.DEAL_MARKET_STATISTICS)
                            {
                                System.out.println("x-bond deal record.");
                                log.info("x-bond deal record.");
                                basicQuote = XBondMarketDataParse.parseMarketDataSnapshotFullRefresh(mdMessage);
                            }
                            //汇总行情
                            else if(tradeMethod ==TradeMethod.ALL ){  //&& mdSubType.equals(MDSubType.DEAL_MARKET_STATISTICS)
                                System.out.println("bond summary");
                                log.info("bond summary");
                                basicQuote = BondMarketDataSummaryParse.parseMarketDataSnapshotFullRefresh(mdMessage);

                            }
                            //质押式回购
                        }else if ( marketInd.equals(MarketIndicator.COLLATERAL_REPO)) {

                        }else  if ( marketInd.equals(MarketIndicator.INTEREST_RATE_SWAP)){
                            //0--成交行情
                            int mdSubType = types.getInt(MDSubType.FIELD);
                            System.out.println("Market test1:" +mdSubType);
                            log.info("Market test1:" +mdSubType);
                            //x-swap
                            if (mdSubType == MDSubType.DEAL_MARKET_STATISTICS ){

                                int tradeMethod = types.getInt(TradeMethod.FIELD);
                                System.out.println("Market test2:" +tradeMethod);
                                log.info("Market test2:" +tradeMethod);
                                //3-匿名点击
                                if (tradeMethod == TradeMethod.MATCHING_NORMAL){
                                    System.out.println("Market xswap.");
                                    log.info("Market xswap.");
                                    basicQuote = XSWAPMarketDataParse.parseMarketDataSnapshotFullRefresh(mdMessage);
                                }

                            }
                            //成交行情 固定-浮动成交 汇总.
                            else  if (mdSubType == MDSubType.DEAL_MARKET_STATISTICS_FIXED_VS_FLOATING ){
                                // System.out.println("xxxxx8 not");
                            }

                        }
                    }else if(mdType == MDType.ON_SHIBOR){
                        //基准指标 shibor数据 shibor值业务要素
                        log.info("Shibor_Cny");
//                        basicQuote= ShiborCnyParse.parseMarketDataSnapshotFullRefresh(mdMessage);

                    }else if(mdType == MDType.SHIBOR_AVERAGES){
                        //基准指标 shibor数据 shibor移动均值业务要素
                        log.info("Shibor_Averages_Cny");
//                        basicQuote= ShiborAveragesCnyParse.parseMarketDataSnapshotFullRefresh(mdMessage);

                    }else if(mdType == MDType.LPR_DATA){
                        //基准指标 贷款市场报价利率数据 LPR业务要素
                        log.info("lpr_Data_Cny");
//                        basicQuote= LprDataCnyParse.parseMarketDataSnapshotFullRefresh(mdMessage);

                    }else if(mdType == MDType.REPO_FIXING_RATE){
                        //基准指标 回购定盘利率数据 回购定盘利率数业务要素&银银间
                        log.info("Repo_Fixing_Rate_Cny");
//                        basicQuote= RepoFixingRateCnyParse.parseMarketDataSnapshotFullRefresh(mdMessage);
                    }
                }
            }
            else if (message instanceof Quote) {
                //basicQuote = parseExecutionReport((ExecutionReport) message);

            }else if (message instanceof NewOrderSingle) {
                //basicQuote = parseExecutionReport((ExecutionReport) message);

            }
            else if (message instanceof MassQuote) {
                //基准指标 shibor数据 shibor报价行报价业务要素
                log.info("Shibor_i_Cny");
            }
            else if (message instanceof ExecutionReport) {
                ExecutionReport executionReport = (ExecutionReport)message;
                //市场类型
                String market=message.getString(MarketIndicator.FIELD).toUpperCase().trim();

                if(market ==null){
                    System.out.println("ExecutionReport,non market:"+message.toString());
                    log.info("ExecutionReport,non market:"+message.toString());
                    return;
                }else if(market.equals(MarketIndicator.COLLATERAL_REPO)){

                }else if(market.equals(MarketIndicator.CASH_BOND)){
                    System.out.println("ExecutionReport: CASH BOND");
                    log.info("ExecutionReport: CASH BOND");
                    basicQuote = BondDealParse.parseExecutionReport(executionReport);
                }else if(market.equals(MarketIndicator.INTEREST_RATE_SWAP)){

                    System.out.println("ExecutionReport: IRS");
                    log.info("ExecutionReport: IRS");
                    basicQuote = IRSDealParse.parseExecutionReport(executionReport);
                    //basicQuote = parseExecutionReport((ExecutionReport) message);
                }else{
                    System.out.println("ExecutionReport,other market:"+market);
                    return;
                }


            }

            if (basicQuote != null){
                //set
                imix.Message.Header header = message.getHeader();
                String senderCompID = header.getString(SenderCompID.FIELD);
                basicQuote.setSenderCompID(senderCompID);
                //
//                log.info("save basicQuote to DB");
                MessageMemory.put(basicQuote);
//                log.info(basicQuote.toString());
            }

        } catch (FieldNotFound fieldNotFound) {
            fieldNotFound.printStackTrace();
            log.info(fieldNotFound.getMessage(),fieldNotFound);
        } catch (IncorrectTagValue incorrectTagValue) {

            incorrectTagValue.printStackTrace();
            log.info(incorrectTagValue.getMessage(),incorrectTagValue);
        } catch (UnsupportedMessageType unsupportedMessageType) {
            unsupportedMessageType.printStackTrace();
            log.info(unsupportedMessageType.getMessage(),unsupportedMessageType);
        } catch (Exception e){
            e.printStackTrace();
            log.info(e.getMessage(),e);
        }

    }

    @Async("saveExecutor")
    public void saveToDB(String Type, List list){

    }
}
